import comm from "comm";
cc.Class({
    extends: comm,

    properties: {
        time:0,//计时
        enemyNode:cc.Node,
        bulletsNode:cc.Node,
    },

    onLoad(){
        G_wuxingfenghuolu.gameObj = this;
        this.coin = 100000;
        this.infoNode = this.node.getChildByName("Bg").getChildByName("info");
        this.enemyZhen = 1000;//阵型敌人计数
        this.huobiRef();
    },




    //刷新货币
    huobiRef(){
        this.infoNode.getChildByName("huobi").getChildByName("jinbi").getComponent(cc.Label).string = "金币："+ this.coin;
        this.infoNode.getChildByName("huobi").getChildByName("zuanshi").getComponent(cc.Label).string = "钻石："+ G_wuxingfenghuolu.userInfo.zuanshi;
    },


    update(dt){
        this.time += dt;
        if(this.time>1){
            this.time=0;
            this.enemyZhen++;
            this.createEnemys();//创建敌人
        }
    },

    createEnemys(){
        //阵型敌人
        if(this.enemyZhen%19==0){
            this.createEnemys_zhen1();
        }
        if(this.enemyZhen%31==0){
            this.createEnemys_zhen2();
        }
        if(this.enemyZhen%123==0){
            this.createEnemys_zhen3();
        }
        if(this.enemyZhen%601==0){
            this.createEnemys_zhen4();
        }
        if(this.enemyZhen%954==0){
            this.createEnemys_zhen5();
        } 
        
        //关卡敌人
        this.createOneEnemy();
    },

    createEnemys_zhen1(){

    },
    createEnemys_zhen2(){
        
    },
    createEnemys_zhen3(){
        
    },
    createEnemys_zhen4(){
        
    },
    createEnemys_zhen5(){
        
    },
    /*
        刷新权重：
        1-5    1
        6-10   2
        11-15  3
        16-20  4
        其他    5
    */
    createOneEnemy(){
        // if(this.enemyNode.childrenCount>0){
        //     return;
        // }

        var enemyName = "prefab_enemy";
        var quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["1"]
        if(G_wuxingfenghuolu.userInfo.gk<=5){
            quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["1"]
        }else if(G_wuxingfenghuolu.userInfo.gk>5 && G_wuxingfenghuolu.userInfo.gk<=10){
            quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["2"]
        }else if(G_wuxingfenghuolu.userInfo.gk>10 && G_wuxingfenghuolu.userInfo.gk<=15){
            quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["3"]
        }else if(G_wuxingfenghuolu.userInfo.gk>15 && G_wuxingfenghuolu.userInfo.gk<=20){
            quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["4"]
        }else{
            quanzhong = G_wuxingfenghuolu.configJson.enemyQuanzhong["4"]
        }

        var allHe = quanzhong[0]+quanzhong[1]+quanzhong[2];
        var suijiE = this.random(0,allHe);
        if(suijiE<=quanzhong[0]){
            enemyName += this.random(1,7);
        }else if(suijiE>quanzhong[0] && suijiE<quanzhong[0]+quanzhong[1]){
            enemyName += this.random(8,9);
        }else{
            enemyName += this.random(10,12);
        }
        var enemy = null;
        enemy = cc.instantiate(cc.globalRes[enemyName]);

        var ranpos = this.random(0,3);
        var x = 0;
        var y = 0;
        switch(ranpos){
            case 0:
                x = -cc.winSize.width/2-20
                y = this.random(-cc.winSize.height/2-20,cc.winSize.height/2+20)
            break;
            case 1:
                x = cc.winSize.width/2+20
                y = this.random(-cc.winSize.height/2-20,cc.winSize.height/2+20) 
            break;
            case 2:
                y = -cc.winSize.height/2-20
                x = this.random(-cc.winSize.width/2-20,cc.winSize.width/2+20)
            break;
            case 3:
                y = cc.winSize.height/2+20
                x = this.random(-cc.winSize.width/2-20,cc.winSize.width/2+20) 
            break;
        }
        enemy.position = cc.v2(x,y);
        this.enemyNode.addChild(enemy);
    },


    findReEnemy(node,fanwei){
        var enemys = this.enemyNode.children.map(enemy => {
            return {
                enemy: enemy,
                distance: node.position.sub(enemy.position).mag() // 计算距离
            };
        });
        enemys.sort((a, b) => a.distance - b.distance);
        var backData = [];
        for(var i = 0;i<enemys.length;i++){
            if(enemys[i].distance<fanwei){
                backData.push(enemys[i].enemy)
            }
        }
        return backData;
    },

    findBloodEnemy(node,fanwei){
        var enemys = this.enemyNode.children.map(enemy => {
            return {
                enemy: enemy,
                blood: enemy.getComponent(enemy.name).getData().maxBlood, // 计算距离
                distance: node.position.sub(enemy.position).mag() // 计算距离
            };
        });
        enemys.sort((a, b) => b.blood - a.blood);
        var backData = [];
        for(var i = 0;i<enemys.length;i++){
            if(enemys[i].distance<fanwei){
                backData.push(enemys[i].enemy)
            }
        }
        return backData;
    },

 


});
